﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;

public partial class Pages_AddSize : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["loginClient"] != null)
            {
                Customer cus = (Customer)Session["loginClient"];

                if (cus.C_Status)
                {
                    int imageid = Convert.ToInt32(Request.Params["id"]);
                    ImagesUI img = new ImagesUI();
                    DataSet dsImg = img.Get(imageid);
                    string filename = dsImg.Tables[0].Rows[0]["I_URL"].ToString();
                    Image1.ImageUrl = "../" + filename;

                    SizeUI size = new SizeUI();
                    cbxSize.DataTextField = "Size_Name";
                    cbxSize.DataValueField = "Size_ID";
                    cbxSize.DataSource = size.GetAll();
                    cbxSize.DataBind();

                }
                else
                {
                    Response.Redirect("../Pages/AccountLock.aspx");
                }
            }
            else
            {
                Response.Redirect("../Pages/Login.aspx");
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Hashtable htb = (Hashtable)Session["Cart"];
            int imageid = Convert.ToInt32(Request.Params["id"]);
            int sizeid = Convert.ToInt32(cbxSize.SelectedValue.ToString());
            int quantity = Convert.ToInt32(txtQuantity.Text.Trim());
            ImagesUI img = new ImagesUI();
            DataSet dsImg = img.Get(imageid);
            string imageurl = dsImg.Tables[0].Rows[0]["I_URL"].ToString();
            string imagename = dsImg.Tables[0].Rows[0]["I_Name"].ToString();
            KeyProduct key = new KeyProduct(sizeid, imageid);
            SizeUI size = new SizeUI();
            DataSet ds = size.Get(sizeid);
            float price = float.Parse(ds.Tables[0].Rows[0]["Price"].ToString());


            if (htb == null)
            {
                htb = new Hashtable();
                htb.Add(key, new Product(sizeid, imagename, imageurl, imageid, price, quantity, quantity * price));
            }
            else
            {
                bool b = false;
                Product te = null;
                foreach (KeyProduct item in htb.Keys)
                {
                    if (item.Size_id == sizeid && item.Image_id == imageid)
                    {
                        b = true;
                        te = (Product)htb[item];
                    }
                }

                if (b)
                {
                    Product p = te;
                    p.Quantity += quantity;
                    p.Total += quantity * price;
                }
                else
                {
                    htb.Add(key, new Product(sizeid, imagename, imageurl, imageid, price, quantity, quantity * price));
                }
            }

            Session["Cart"] = htb;
            
        }
        catch (Exception ex)
        {

        }

    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Pages/MyAlbum.aspx");
    }
}
